package cn.edu.nju.newsFeed;

import java.util.ArrayList;
import java.util.List;

import com.devsmart.android.HorizontalListView;

import cn.edu.nju.R;
import cn.edu.nju.setting.ExpandableHeightGridView;
import cn.edu.nju.setting.ModifiableTagAdapter;
import cn.edu.nju.setting.TagAdapter;
import cn.edu.nju.setting.TagCategoryListAdapter;
import cn.edu.nju.setting.TagProvider;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class NewsFeedTagActivity extends Activity{
	private ArrayList<String> tags;
	private ModifiableTagAdapter tagAdapter;
	private TagAdapter recommendTagAdapter;
	private TagCategoryListAdapter tagCategoryListAdapter;
	private HorizontalListView tagCategoryListView;
	private ExpandableHeightGridView tagsLayout;
	private ExpandableHeightGridView tagPanel;
	private List<String> recommendTags;
	private TextView selectedCategory;
	private TagProvider tagProvider;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.post_tag);
		
		Intent intent = getIntent();
		tags = intent.getStringArrayListExtra(WritePostActivity.POST_TAGS);
		
		tagProvider = TagProvider.getInstance(this);
		
		// init widget
		initSaveButton();
		initCancelButton();
		initRuleTags();
		initRecommendTags();
	}
	
	private void initSaveButton(){
		Button saveButton = (Button)findViewById(R.id.save_button);
		saveButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.putStringArrayListExtra(WritePostActivity.POST_TAGS, tags);
				setResult(RESULT_OK, intent);
				finish();
			}
		});
	}
	
	private void initCancelButton(){
		Button cancelButton = (Button)findViewById(R.id.cancel_button);
		cancelButton.setOnClickListener(new OnClickListener() {		
			public void onClick(View v) {
				setResult(RESULT_CANCELED);
				finish();
			}
		});
	}
	
	private void initRuleTags(){
		tagAdapter = new ModifiableTagAdapter(this, tags);
		tagsLayout = (ExpandableHeightGridView) findViewById(R.id.post_tags_layout);
		tagsLayout.setAdapter(tagAdapter);
	}
	
	private void initRecommendTags(){
		tagCategoryListView = (HorizontalListView) findViewById(R.id.tag_category_list);
		tagCategoryListAdapter = new TagCategoryListAdapter(this, tagCategoryListView);
		tagCategoryListView.setAdapter(tagCategoryListAdapter);
		tagPanel = (ExpandableHeightGridView) findViewById(R.id.tag_section);
		
		tagCategoryListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long id) {
				// cancel the highlight of the previous category and highlight current selected category
				if(selectedCategory == null){
					// trick it is the first category
					selectedCategory = (TextView)(tagCategoryListView.getChildAt(0).findViewById(R.id.category_text));
				} 
				selectedCategory.setBackgroundResource(R.drawable.rounded_rectangle);
				selectedCategory.setTextColor(R.color.tag_font_color);
				
				selectedCategory = (TextView)view.findViewById(R.id.category_text);
				
				selectedCategory.setBackgroundResource(R.drawable.selected_rounded_rectangle);
				selectedCategory.setTextColor(R.color.white);
				initTagSection(position);
				
				tagCategoryListAdapter.setCurrentSelected(position);
			}			
		});
		
		// default select first category
		tagCategoryListAdapter.setCurrentSelected(0);
		initTagSection(0);
	}
	
	// show tags in category whose index is position
	protected void initTagSection(int position){
		recommendTags = tagProvider.getTagsInCategory(position);
		recommendTagAdapter = new TagAdapter(this, recommendTags);
		tagPanel.setAdapter(recommendTagAdapter);
		
		tagPanel.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				String tag = recommendTags.get(position);
				if(!tags.contains(tag)){
					tags.add(tag);
					tagAdapter.notifyDataSetChanged();
				}
			}
		});
	}
}
